﻿// Fill out your copyright notice in the Description page of Project Settings.


#include "AIController/Enemy2AIController.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "Character/EnemyCharacter.h"
#include "Character/PlayerCharacter.h"
#include "Character/PlayerCharacter.h"
#include "Helper/DebugHelper.h"
#include "Perception/AIPerceptionComponent.h"


// Sets default values
AEnemy2AIController::AEnemy2AIController()
{
	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	AIPerceptionComponent = CreateDefaultSubobject<UAIPerceptionComponent>("AIPerceptionComponent");
}

// Called when the game starts or when spawned
void AEnemy2AIController::BeginPlay()
{
	Super::BeginPlay();
	//Debug::printLog("enemyAi2 on beginPlay");
	//绑定当感知到player处理的方法
	// AIPerceptionComponent->OnTargetPerceptionUpdated.AddDynamic(this, &AEnemy2AIController::OnTargetPerceptionUpdated);
}

void AEnemy2AIController::OnPossess(APawn* InPawn)
{
	//Debug::printLog("enemyAi2 on possess");
	Super::OnPossess(InPawn);
	//绑定当感知到player处理的方法
	AIPerceptionComponent ->OnTargetPerceptionUpdated.AddDynamic(this, &AEnemy2AIController::OnTargetPerceptionUpdated);

}

// Called every frame
void AEnemy2AIController::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

void AEnemy2AIController::OnTargetPerceptionUpdated(AActor* Actor, FAIStimulus Stimulus)
{
	APlayerCharacter* player = Cast<APlayerCharacter>(Actor);
	if (player)
	{
		UBlackboardComponent* BlackboardComponent = GetBlackboardComponent();

		if (BlackboardComponent)
		{
			BlackboardComponent->SetValueAsObject(FName("PlayerPawn"), player);
		}
	}
}
